home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / TOWERS.C < prev    next >
Text File  |  1987-06-18  |  5KB  |  195 lines

  1.  
  2. /*
  3.  *                 IBM RBBS-PC Tulsa, OK
  4.  *                 Switching totally to the "C" Language
  5.  *                 24 Hour operation 300/1200 baud XMODEM
  6.  *                 918-664-8737
  7.  *                 SYSOP LYNN LONG
  8.  *
  9.  *                  Towers of Hanoi
  10.  *
  11.  * The object of the game is to transfer the disks from
  12.  * the leftmost tower to the rightmost tower obeying the
  13.  * following set of rules:
  14.  *
  15.  * 1) Only the top disk of any tower may be moved at a time
  16.  * 2) At no time must a larger disk be placed on a smaller disk
  17.  *
  18.  */
  19.  
  20. #define FORMFEED                0x0c
  21. #define ESC                             0x1b
  22.  
  23. #define POST                    0xba
  24. #define POST_BASE               0xca
  25. #define BASE                    0xcd
  26. #define RING                    0xdc
  27.  
  28. #define SCREEN_WIDTH    80
  29. #define SCREEN_HEIGHT   25
  30.  
  31. #define RING_WIDTH              ((((SCREEN_WIDTH - 2)/3) & 0xfe)-1)
  32. #define LEFT_POST               (RING_WIDTH/2+1)
  33. #define CENTER_POST             (LEFT_POST+RING_WIDTH)
  34. #define RIGHT_POST              (LEFT_POST+2*RING_WIDTH)
  35.  
  36. #define MOVING_ROW              2
  37. #define BASE_ROW                15
  38. #define POST_HEIGHT             11
  39.  
  40. char top[3] { BASE_ROW-1, BASE_ROW-1, BASE_ROW-1 };
  41. int pause 0;
  42.  
  43.  
  44. main(argc, argv)
  45. int argc;
  46. char *argv[];
  47.         {
  48.         int nrings;
  49.  
  50.         if(argc < 1  ||  argc > 3)
  51.                 abort("Use: hanoi [rings [delay]]\n");
  52.  
  53.         nrings = argc > 1 ? atoi(argv[1]) : 7;
  54.         pause = argc > 2 ? atoi(argv[2]) : 1;
  55.  
  56.         setup(nrings);
  57.         hanoi(nrings, 0, 2, 1);
  58.  
  59.         curse(0, SCREEN_HEIGHT-1);
  60.         }
  61.  
  62.  
  63. hanoi(n, a, b, c)
  64. char n, a, b, c;
  65.         {
  66.         if(n == 0)
  67.                 return;
  68.  
  69.         hanoi(n-1, a, c, b);
  70.         movering(n, a, b);
  71.         hanoi(n-1, c, b, a);
  72.         }
  73.  
  74.  
  75. setup(n)
  76. char n;
  77.         {
  78.         char i;
  79.  
  80.         putchar(FORMFEED);
  81.  
  82.         for(i = MOVING_ROW+2; i < BASE_ROW; ++i) {
  83.                 cput(LEFT_POST, i, POST);
  84.                 cput(CENTER_POST, i, POST);
  85.                 cput(RIGHT_POST, i, POST);
  86.                 }
  87.  
  88.         curse(0, BASE_ROW);
  89.  
  90.         for(i = 1; i < SCREEN_WIDTH; ++i)
  91.                 putchar(BASE);
  92.  
  93.         cput(LEFT_POST, BASE_ROW, POST_BASE);
  94.         cput(CENTER_POST, BASE_ROW, POST_BASE);
  95.         cput(RIGHT_POST, BASE_ROW, POST_BASE);
  96.  
  97.         for(i = n; i > 0; --i)
  98.                 draw(i, LEFT_POST, top[0]--, RING);
  99.         }
  100.  
  101.  
  102. curse(x, y)
  103. char x, y;
  104.         {
  105.  
  106.         wait();
  107.  
  108.         putchar(ESC);
  109.         putchar('=');
  110.         putchar(y + ' ');
  111.         putchar(x + ' ');
  112.         }
  113.  
  114.  
  115. cput(x, y, ch)
  116. char ch;
  117. int x,y;
  118.         {
  119.         putchar(ESC);
  120.         putchar('=');
  121.         putchar(y + ' ');
  122.         putchar(x + ' ');
  123.         putchar(ch);
  124.         wait();
  125.         }
  126.  
  127.  
  128. draw(ring, centre, y, ch)
  129. char ring, centre, y, ch;
  130.         {
  131.         char i;
  132.  
  133.         curse(centre-ring, y);
  134.  
  135.         for(i=0; i<ring; ++i)
  136.                 putchar(ch);
  137.  
  138.         curse(centre+1, y);
  139.  
  140.         for(i=0; i<ring; ++i)
  141.                 putchar(ch);
  142.         }
  143.  
  144.  
  145. movering(ring, from, to)
  146. char ring, from, to;
  147.         {
  148.         char fromc, toc;
  149.         char fromy, toy;
  150.  
  151.         fromc = LEFT_POST + from * RING_WIDTH;
  152.         toc = LEFT_POST + to * RING_WIDTH;
  153.         fromy = ++top[from];
  154.         toy = top[to]--;
  155.  
  156.         while(fromy != MOVING_ROW) {
  157.                 draw(ring, fromc, fromy, ' ');
  158.                 draw(ring, fromc, --fromy, RING);
  159.                 }
  160.  
  161.         if(fromc < toc)
  162.                 while(fromc != toc) {
  163.                         cput(fromc-ring, fromy, ' ');
  164.                         cput(fromc, fromy, RING);
  165.                         cput(fromc+1, fromy, ' ');
  166.                         cput(fromc+ring+1, fromy, RING);
  167.                         ++fromc;
  168.                         }
  169.         else if (fromc > toc)
  170.                 while(fromc != toc) {
  171.                         cput(fromc+ring, fromy, ' ');
  172.                         cput(fromc, fromy, RING);
  173.                         cput(fromc-1, fromy, ' ');
  174.                         cput(fromc-ring-1, fromy, RING);
  175.                         --fromc;
  176.                         }
  177.  
  178.         while(fromy != toy) {
  179.                 draw(ring, fromc, fromy, ' ');
  180.                 draw(ring, fromc, ++fromy, RING);
  181.                 }
  182.         }
  183.  
  184.  
  185. wait() {
  186.         int i;
  187.  
  188.         i = 1 << pause;
  189.  
  190.         while(--i)
  191.                 i = i;
  192.         }
  193.  
  194.  
  195.